Input and Output in PyBigDFT

There are a number of standard file types that PyBigDFT can work with using its IO module. Here we will demonstrate some of that capability.

XYZ Files

The XYZReader class has access to some built in molecules, so let’s begin there. You can access any of the molecules in the database just by opening an XYZReader with that name. Or you can specify a filename path to get something you’ve already made yourself.

[1]:
from BigDFT.IO import XYZReader
from BigDFT.Systems import System
from BigDFT.Fragments import Fragment

sys = System()
sys["SI4:0"] = Fragment()
with XYZReader("CH4") as ifile:
    for atom in ifile:
        sys["SI4:0"].append(atom)

sys["CH2F:1"] = Fragment()
with XYZReader("CH2F") as ifile:
    for atom in ifile:
        sys["CH2F:1"].append(atom)

sys["CH2F:1"].translate([-5, 0, 0])

Let’s look at what we built.

[2]:
from BigDFT.Visualization import InlineVisualizer
viz = InlineVisualizer(500,400)
viz.display_system(sys)

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol

The XYZWriter works in a similar way.

[3]:
from BigDFT.IO import XYZWriter
with XYZWriter("test.xyz", units="angstroem", natoms=sum([len(x) for x in sys.values()])) as ofile:
    for frag in sys.values():
        for at in frag:
            ofile.write(at)

Other Formats

The other formats work in a more straightforward way on file handles. For example, let’s say we want to write a PDB file.

[4]:
from BigDFT.IO import write_pdb
with open("test.pdb", "w") as ofile:
    write_pdb(sys, ofile)

Let’s test if that worked by loading the written file into a string.

[5]:
with open("test.pdb") as ifile:
    for line in ifile:
        print(line, end="")
HETATM    1 C    SI4 A   0       0.000   0.000   0.000  1.00  0.00       B   C
HETATM    2 H    SI4 A   0       0.628   0.628   0.628  1.00  0.00       B   H
HETATM    3 H    SI4 A   0       0.628  -0.628  -0.628  1.00  0.00       B   H
HETATM    4 H    SI4 A   0      -0.628   0.628  -0.628  1.00  0.00       B   H
HETATM    5 H    SI4 A   0      -0.628  -0.628   0.628  1.00  0.00       B   H
HETATM    6 C    CH2 A   1      -2.675   0.655   0.000  1.00  0.00       B   C
HETATM    7 F    CH2 A   1      -2.675  -0.682   0.000  1.00  0.00       B   F
HETATM    8 H    CH2 A   1      -2.431   1.104   0.947  1.00  0.00       B   H
HETATM    9 H    CH2 A   1      -2.431   1.104  -0.947  1.00  0.00       B   H

And of course we can read in a system like this.

[6]:
from BigDFT.IO import read_pdb
with open("test.pdb") as ifile:
    sys2 = read_pdb(ifile)
[7]:
for fragid, frag in sys2.items():
    print(fragid)
    for at in frag:
        print(dict(at))
SI4:0
{'C': [0.0, 0.0, 0.0], 'name': 'C', 'units': 'angstroem'}
{'H': [0.628, 0.628, 0.628], 'name': 'H', 'units': 'angstroem'}
{'H': [0.628, -0.628, -0.628], 'name': 'H', 'units': 'angstroem'}
{'H': [-0.628, 0.628, -0.628], 'name': 'H', 'units': 'angstroem'}
{'H': [-0.628, -0.628, 0.628], 'name': 'H', 'units': 'angstroem'}
CH2:1
{'C': [-2.675, 0.655, 0.0], 'name': 'C', 'units': 'angstroem'}
{'F': [-2.675, -0.682, 0.0], 'name': 'F', 'units': 'angstroem'}
{'H': [-2.431, 1.104, 0.947], 'name': 'H', 'units': 'angstroem'}
{'H': [-2.431, 1.104, -0.947], 'name': 'H', 'units': 'angstroem'}

It is possible to write xyz files this way as well.

[8]:
from io import StringIO
from BigDFT.IO import write_xyz

outstr = StringIO()
write_xyz(sys2, outstr)
print(outstr.getvalue())
9 angstroem
free
C 0.0 0.0 0.0
H 0.628 0.628 0.628
H 0.628 -0.628 -0.628
H -0.628 0.628 -0.628
H -0.628 -0.628 0.628
C -2.675 0.6549999999999999 0.0
F -2.675 -0.682 0.0
H -2.431 1.104 0.947
H -2.431 1.104 -0.947